<template>
    <!-- 新增外层容器承载页面背景 -->
    <div class="monitor-chart-page">
        <el-row>
            <el-col :span="5">
                <div class="tree">
                    <el-input placeholder="输入关键字进行过滤" v-model="filterText">
                    </el-input>
                    <el-tree @node-click="handlerClick" class="filter-tree" :data="EngData" :props="defaultProps"
                        :filter-node-method="filterNode" ref="tree">
                    </el-tree>
                </div>
            </el-col>
            <el-col :span="19">
                <!-- 图表容器：添加白色背景，确保图表清晰展示 -->
                <div id="container"
                    style="height: 150px; background-color: #fff; border-radius: 8px; padding: 10px; margin-bottom: 15px;">
                </div>

                <div v-if="params.device_id" class="table">
                    <el-form :inline="true" :model="form">
                        <el-form-item>
                            <el-date-picker value-format="timestamp" v-model="form.time" type="datetimerange"
                                :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期"
                                end-placeholder="结束日期" align="right">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="toSearch">搜索</el-button>
                        </el-form-item>
                    </el-form>
                    <!-- 表格  -->
                    <el-table :data="monitorData" style="width: 100%" :row-class-name="tableRowClassName">
                        <el-table-column prop="insert_time" label="日期" width="180" align="center">
                            <template v-slot="scope">
                                <div>
                                    {{ fmtDate(scope.row.insert_time) }}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="tsp" label="tsp" width="180">
                        </el-table-column>
                        <el-table-column prop="pm10" label="pm10">
                        </el-table-column>
                        <el-table-column prop="pm25" label="pm2.5">
                        </el-table-column>
                        <el-table-column prop="temperature" label="温度">
                        </el-table-column>
                        <el-table-column prop="humidity" label="湿度">
                        </el-table-column>
                        <el-table-column prop="wind_direction" label="风向">
                        </el-table-column>
                        <el-table-column prop="wind_speed" label="风速">
                        </el-table-column>
                    </el-table>

                    <!-- 分页组件 -->
                    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page="params.page" :page-sizes="[10, 20, 50, 100]" :page-size="params.pageSize"
                        layout="total, sizes, prev, pager, next, jumper" :total="total" style="margin-top: 15px;">
                    </el-pagination>
                </div>
                <div v-else class="empty-tip">请点击设备查看数据</div>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import { get } from '@/utils/request'
import { Area } from '@antv/g2plot'
export default {
    data() {
        return {
            filterText: '',
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            monitorData: [],
            form: { time: [] },
            EngData: [],
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            params: {
                page: 1,
                pageSize: 10,
                device_id: 0,
                engineer_id: 0
            },
            total: 0 // 总数据条数
        }
    },
    watch: {
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    },
    methods: {
        loadArea() {
            const area = new Area('container', {
                data: [
                    { "timePeriod": "2006 Q3", "value": 1 },
                    { "timePeriod": "2006 Q4", "value": 1.08 },
                    { "timePeriod": "2007 Q1", "value": 1.17 },
                    { "timePeriod": "2007 Q2", "value": 1.26 },
                    { "timePeriod": "2007 Q3", "value": 1.34 },
                    { "timePeriod": "2007 Q4", "value": 1.41 },
                    { "timePeriod": "2008 Q1", "value": 1.52 },
                    { "timePeriod": "2008 Q2", "value": 1.67 },
                    { "timePeriod": "2008 Q3", "value": 1.84 },
                    { "timePeriod": "2008 Q4", "value": 2.07 },
                    { "timePeriod": "2009 Q1", "value": 2.39 },
                    { "timePeriod": "2009 Q2", "value": 2.71 },
                    { "timePeriod": "2009 Q3", "value": 3.03 },
                    { "timePeriod": "2009 Q4", "value": 3.33 },
                    { "timePeriod": "2010 Q1", "value": 3.5 },
                    { "timePeriod": "2010 Q2", "value": 3.37 },
                    { "timePeriod": "2010 Q3", "value": 3.15 },
                    { "timePeriod": "2010 Q4", "value": 3.01 },
                    { "timePeriod": "2011 Q1", "value": 2.8 },
                    { "timePeriod": "2011 Q2", "value": 2.8 },
                    { "timePeriod": "2011 Q3", "value": 2.84 },
                    { "timePeriod": "2011 Q4", "value": 2.75 },
                    { "timePeriod": "2012 Q1", "value": 2.64 },
                    { "timePeriod": "2012 Q2", "value": 2.55 },
                    { "timePeriod": "2012 Q3", "value": 2.46 },
                    { "timePeriod": "2012 Q4", "value": 2.45 },
                    { "timePeriod": "2013 Q1", "value": 2.57 },
                    { "timePeriod": "2013 Q2", "value": 2.68 },
                    { "timePeriod": "2013 Q3", "value": 2.8 },
                    { "timePeriod": "2013 Q4", "value": 2.89 },
                    { "timePeriod": "2014 Q1", "value": 2.85 },
                    { "timePeriod": "2014 Q2", "value": 2.73 },
                    { "timePeriod": "2014 Q3", "value": 2.54 },
                    { "timePeriod": "2014 Q4", "value": 2.32 },
                    { "timePeriod": "2015 Q1", "value": 2.25 },
                    { "timePeriod": "2015 Q2", "value": 2.33 },
                    { "timePeriod": "2015 Q3", "value": 2.53 },
                    { "timePeriod": "2015 Q4", "value": 2.74 },
                    { "timePeriod": "2016 Q1", "value": 2.76 },
                    { "timePeriod": "2016 Q2", "value": 2.61 },
                    { "timePeriod": "2016 Q3", "value": 2.35 },
                    { "timePeriod": "2016 Q4", "value": 2.11 },
                    { "timePeriod": "2017 Q1", "value": 2.08 },
                    { "timePeriod": "2017 Q2", "value": 2.2 },
                    { "timePeriod": "2017 Q3", "value": 2.38 },
                    { "timePeriod": "2017 Q4", "value": 2.59 },
                    { "timePeriod": "2018 Q1", "value": 2.63 },
                    { "timePeriod": "2018 Q2", "value": 2.67 },
                    { "timePeriod": "2018 Q3", "value": 2.64 },
                    { "timePeriod": "2018 Q4", "value": 2.5 },
                    { "timePeriod": "2019 Q1", "value": 2.31 },
                    { "timePeriod": "2019 Q2", "value": 2.04 },
                    { "timePeriod": "2019 Q3", "value": 1.83 },
                    { "timePeriod": "2019 Q4", "value": 1.71 },
                    { "timePeriod": "2020 Q1", "value": 1.65 },
                    { "timePeriod": "2020 Q2", "value": 1.59 },
                    { "timePeriod": "2020 Q3", "value": 1.58 }
                ],
                xField: 'timePeriod',
                yField: 'value',
                xAxis: {
                    range: [0, 1],
                },
            });
            area.render();
        },
        // 日期格式化方法
        fmtDate(time) {
            if (!time) return '';
            const date = new Date(time);
            return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
        },
        async handlerClick(val, node) {
            if (!val.children) {
                this.params.device_id = val.id;
                this.params.engineer_id = val.engineer_id;
                this.params.page = 1; // 切换设备时重置页码
                await this.getMonitor();
            }
        },
        toSearch() {
            this.params.page = 1;
            this.getMonitor();
        },
        // 处理每页条数变化
        handleSizeChange(val) {
            this.params.pageSize = val;
            this.params.page = 1;
            this.getMonitor();
        },
        // 处理当前页码变化
        handleCurrentChange(val) {
            this.params.page = val;
            this.getMonitor();
        },
        async getMonitor() {
            let temp = { ...this.params };
            if (this.form.time) {
                temp.begin_time = this.form.time[0];
                temp.end_time = this.form.time[1];
            }
            let res = await get('/monitor/pageQuery', temp);
            this.monitorData = res.data.list;
            this.total = res.data.total; // 获取总条数
        },
        async getEngDevData() {
            let res = await get('/engineer/findEngineerDeviceTree');
            this.EngData = res.data;
        },
        filterNode(value, data) {
            if (!value) return true;
            return data.name.indexOf(value) !== -1;
        },
        tableRowClassName({ row, rowIndex }) {
            if (rowIndex % 2 == 0 || rowIndex == 0) {
                return 'warning-row';
            } else {
                return 'success-row';
            }
            return '';
        }
    },
    created() {
        this.getEngDevData();
        this.getMonitor();
    },
    mounted() {
        this.loadArea();
    }
}
</script>
<style lang="scss" scoped>
/* 页面外层容器：粉紫色背景，铺满视口 */
.monitor-chart-page {
    min-height: 100vh;
    background-color: rgba(236, 192, 255, 0.3);
    padding: 15px;
}

.tree {
    height: calc(100vh - 90px); // 适配外层内边距，避免滚动条异常
    padding: 10px;
    border-right: 1px solid #ececec;
    background-color: #fff; // 白色背景提升树形区域可读性
    border-radius: 8px;
    margin-right: 10px;
}

/* 表格区域：白色背景，圆角优化 */
.table {
    background-color: #fff;
    border-radius: 8px;
    padding: 15px;
    box-sizing: border-box;
}

/* 空提示区域：白色背景，与图表/表格区域高度协调 */
.empty-tip {
    line-height: 50px;
    color: red;
    font-size: 20px;
    text-align: center;
    background-color: #fff;
    min-height: 350px; // 匹配图表+表格区域的最小高度
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 10px;
}

::v-deep .el-table .warning-row {
    background: oldlace;
}

::v-deep .el-table .success-row {
    background: #f0f9eb;
}
</style>